using System;
using System.Collections.Generic;
using System.Text;

namespace PACKETPARSERMAX20.CORE
{
    public abstract class StreamBase : IDisposable
    {
        private System.IO.MemoryStream stream = null;
        private System.IO.BinaryReader reader = null;
        private int lastPosition = 0;
        private long customPosition = 0;
        private long customSize = 0;
        private long previousPosition = 0;
        private bool allowClose = true;

        public int AvailableBytes
        {
            get
            {
                return (int)(Stream.Length - Stream.Position);
            }
        }

        public System.IO.MemoryStream Stream
        {
            get
            {
                return stream;
            }
        }

        public System.IO.BinaryReader Reader
        {
            get
            {
                return reader;
            }
        }

        public StreamBase()
        {
        }
 
        ~StreamBase()
        {
            Close();
        }

        public bool Load(byte[] data)
        {
            allowClose = true;
            if (IsOpen) { Close(); }
            stream = new System.IO.MemoryStream(data);
            reader = new System.IO.BinaryReader(stream);
            return true;
        }

        public bool IsOpen
        {
            get
            {
                if (stream == null && reader == null)
                    return false;

                return stream.CanRead;
            }
        }

        public void Close()
        {
            if (allowClose)
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                    stream = null;
                }
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                    reader = null;
                }



            }
        }
 
        public int SetPosition()
        {
            lastPosition = (int)Stream.Position;
            return lastPosition;
        }

        public int ResetPosition()
        {
            Stream.Position = (long)lastPosition;
            return lastPosition;
        }

        public void SetPreviousPosition()
        {
            previousPosition = Stream.Position;
        }

        public void ResetPreviousPosition()
        {
            Stream.Position = previousPosition;
        }

        public void SetCustomPositionAndSize(long position, long size)
        {
            customPosition = position;
            customSize = size;
        }

        public long AvailableBytesCustom
        {
            get
            {
                return CalcAvailableBytesCustom(customPosition, customSize);
            }
        }

        public long CalcAvailableBytesCustom(long position, long size)
        {
            long left_over_size = 0;
            long read_size = Stream.Position - (position - 2);
            if (read_size < (size + 2))
            {
                left_over_size = (size + 2) - read_size;
            }
            return left_over_size;
        }

        void IDisposable.Dispose()
        {
            Close();
        }


    }
}


